package opd.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import opd.db.services.IReport3Dao;
import opd.helpers.ConstantsHolder;
import opd.servlets.helpers.ReportDates;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.google.gson.Gson;

/**
 * Servlet implementation class Report3
 */
@WebServlet(description = "Serves Report 3 with Data", urlPatterns = { "/Report3" })
public class Report3 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@Autowired
	ConstantsHolder constantsHolder;
	
	@Autowired
	IReport3Dao report3Dao;
	
	// ----------------------------------------------------------------------------------------------------
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Report3() {
        super();
    }

    // ----------------------------------------------------------------------------------------------------
	
    @Override
    public void init(ServletConfig config) throws ServletException {
    	super.init(config);
    	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());    	
    }
    
    // ----------------------------------------------------------------------------------------------------
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.getWriter().write(String.format("Report 3: %s", constantsHolder.getDaysBack()));
		
		ReportDates reportDates = new ReportDates(constantsHolder.getDaysBack());
		Gson gson = new Gson();
		
		Date date = reportDates.getLastProductionDate();
		
		
		response.getWriter().write(gson.toJson(report3Dao.getDailyReport(reportDates.getLastProductionDate(), reportDates.getReport3DatesBack())));
	}

	// ----------------------------------------------------------------------------------------------------
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	// ----------------------------------------------------------------------------------------------------
	
	public IReport3Dao getReport3Dao() {
		return report3Dao;
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	public void setReport3Dao(IReport3Dao report3Dao) {
		this.report3Dao = report3Dao;
	}
}
